package com.ybfenpan.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public final static String dateFmt = "yyyy-MM-dd";
	public final static String timeFmt = "yyyy-MM-dd HH:mm:ss";
	public final static int currentYear(){return Calendar.getInstance().get(Calendar.YEAR);};
	public static int currentMonth(){return Calendar.getInstance().get(Calendar.MONTH)+1;};
	
	
	public static String getSysDate(){
		return dateFormat(new Date(), dateFmt);
	}
	
	public static String getSysDate(String fmt){
		return dateFormat(new Date(), fmt);
	}
	
	public static String getSysTime(){
		return dateFormat(new Date(), timeFmt);
	}
	
	/**
     * 从 Date 获得一个格式化的字符串。
     * @see StringUtils#getNowDate(String)
     * @param d d
     * @param format format
     * @return String
     */
    public static String dateFormat(Date d, String format) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            return formatter.format(d);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 从给定的字符串中解析出一个 java.util.Date<br>
     * 字符串的格式为 yyyy-MM-dd（如 2001-07-19）
     * @param str str
     * @return Date
     */
    public static Date parseDate(String str) {
        Date sDate = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(dateFmt);
            sDate = formatter.parse(str);
        } catch (Exception e) {
            return null;
        }
        return sDate;
    }
    
    /**
     * 从 Date 获得一个格式化的Date。
     * @see StringUtils#getNowDate(String)
     * @param d d
     * @param format format
     * @return String
     */
    public static Date parseDateFormat(Object d, String format) {
    	if(null == d) return null;
        try {
        	SimpleDateFormat formatter = new SimpleDateFormat(format);
        	String dStr = null;
        	if(d instanceof Date){
        		dStr = formatter.format(d);
        	}else{
        		dStr = d.toString();
        	}
            return formatter.parse(dStr);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 计算两个dateTime相差几分钟
     * @param dateTime1
     * @param dateTime2
     * @return
     */
    public static long differMins(Date dateTime1, Date dateTime2){
    	Calendar dateOne=Calendar.getInstance();
    	dateOne.setTime(dateTime1);	//设置时间 1
    	Calendar dateTwo=Calendar.getInstance();
		dateTwo.setTime(dateTime2);	//设置时间 2
		long timeOne=dateOne.getTimeInMillis();
		long timeTwo=dateTwo.getTimeInMillis();
		long minute=(timeOne-timeTwo)/(1000*60);//转化minute
		return minute;
    }
    
    /**
     * 计算两个日期相差几天
     * @param dateBegin dateBegin
     * @param dateEnd dateEnd
     * @return long
     * @throws ParseException 
     */
    public static long differDays(String dateBegin, String dateEnd) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(dateFmt);
        Date d1 = df.parse(dateBegin);
        Date d2 = df.parse(dateEnd);
        return (d2.getTime()-d1.getTime())/(3600 * 24 * 1000);
    }
    
    /**
     * 计算两个日期相差几年
     * @param dateBegin
     * @param dateEnd
     * @return
     * @throws ParseException
     */
    public static int differYears(String dateBegin, String dateEnd) throws ParseException {
    	SimpleDateFormat df = new SimpleDateFormat(dateFmt);
        Date date1 = df.parse(dateBegin);
    	Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        
        Date date2 = df.parse(dateEnd);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        
    	int d1 = c1.get(c1.DAY_OF_MONTH);
    	int m1 = c1.get(c1.MONTH) + 1;
    	int y1 = c1.get(c1.YEAR);
    	int d2 = c2.get(c2.DAY_OF_MONTH);
    	int m2 = c2.get(c2.MONTH) + 1;
    	int y2 = c2.get(c2.YEAR);
    	if(d2 < d1){
    		m2--;
    	}
    	if(m2 < m1){
    		y2--;
    	}
    	int retY = y2 - y1;
    	return retY < 0 ? 0 : retY;
    }
    
	/**
	 * d1是否小于d2
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean before(Date d1, Date d2, boolean eqFlag){
		try {
			String dateStr1 = dateFormat(d1, dateFmt);
			String dateStr2 = dateFormat(d2, dateFmt);
			long n = differDays(dateStr1, dateStr2);
			if(n > 0){
				return true;
			}
			else if (eqFlag && 0==n) {
				return true;
			}
			SimpleDateFormat sdf = new SimpleDateFormat(dateFmt);
			d1 = sdf.parse(dateStr1);
			d2 = sdf.parse(dateStr2);
			return d2.after(d1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
     * 将日期加N天得到新日期
     * @param oldDate oldDate
     * @param n n
     * @return String
     */
    public static Date dateAddDay(Date oldDate, int n) {
    	if(oldDate == null) return null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldDate);
        calendar.add(Calendar.DATE, n);
        return calendar.getTime();
    }
    
    /**
     * 将日期加N月得到新日期
     * @param oldDate oldDate
     * @param n n
     * @return String
     */
    public static Date dateAddMonth(Date oldDate, int n) {
    	if(oldDate == null) return null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldDate);
        calendar.add(Calendar.MONTH, n);
        return calendar.getTime();
    }
    
    /**
     * 获取指定日期第一天
     * @param oldDate
     * @return
     */
    public static Date dateFirstDay(Date oldDate){
    	Calendar c = Calendar.getInstance();
    	c.setTime(oldDate);
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return c.getTime();
    }
    
    /**
     * 获取指定日期最后一天
     * @param oldDate
     * @return
     */
    public static Date dateLastDay(Date oldDate){
    	Calendar c = Calendar.getInstance();
    	c.setTime(oldDate);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }
    
    /**
     * 获取指定年/月的最后一天
     * @param year
     * @param month
     * @return
     */
    public static String monthLastday(int year, int month){
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.YEAR,year);
		ca.set(Calendar.MONTH,month-1);
		ca.set(Calendar.DATE, 1);
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat format = new SimpleDateFormat(dateFmt);
        return format.format(ca.getTime());
	}

	/**
	 * 获取某年某周的第一天
	 * @return:Date
	 * @throws
	 */
	public static Date weekFirstDay(int year, int month, int week) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,year);
		cal.set(Calendar.MONTH,month);
		cal.set(Calendar.WEEK_OF_MONTH, week);
		//设置该周第一天为星期一
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}

	/**
	 * 获取某年某周的第一天
	 * @return:Date
	 * @throws
	 */
	public static Date weekLastDay(int year, int month, int week) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,year);
		cal.set(Calendar.MONTH,month);
		cal.set(Calendar.WEEK_OF_MONTH, week);
		//设置该周第一天为星期一
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return cal.getTime();
	}
	/**
	 * 获取当前日期是星期几<br>
	 * @param dt
	 * @return 当前日期是星期几
	 */
	public static int getWeekOfDate(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		return cal.get(Calendar.DAY_OF_WEEK)-1;
	}

}
